package com.beesphere.products.throttling.processor;

import com.beesphere.products.throttling.UnitOfWork;

public class DelegateProcessor implements Processor {

	protected Processor processor;

    public DelegateProcessor() {
    }

    public DelegateProcessor (Processor processor) {
        if (processor == this) {
            throw new IllegalArgumentException("Recursive DelegateProcessor!");
        }
        this.processor = processor;
    }

    public void process (UnitOfWork unitOfWork) throws Exception {
        processNext (unitOfWork);
    }

    protected void processNext (UnitOfWork unitOfWork) throws Exception {
        if (processor != null) {
            processor.process (unitOfWork);
        }
    }

    @Override
    public String toString() {
        return "Delegate(" + processor + ")";
    }

    public Processor getProcessor() {
        return processor;
    }

    public void setProcessor(Processor processor) {
        this.processor = processor;
    }

    /**
     * Proceed with the underlying delegated processor
     */
    public void proceed (UnitOfWork unitOfWork) throws Exception {
        processNext (unitOfWork);
    }
    
    public void start () throws Exception {
    	processor.start ();
    }
    
    public void stop () throws Exception {
    	processor.stop ();
    }

}
